home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Libraries / Framework / Includes / UCommand.h < prev    next >
Encoding:
Text File  |  1996-04-03  |  15.0 KB  |  441 lines  |  [TEXT/MPS ]

  1. // UCommand.h
  2. // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
  3.  
  4.  
  5. #ifndef __UCOMMAND__
  6. #define __UCOMMAND__
  7.  
  8. // MacApp
  9.  
  10. #ifndef __UEVENT__
  11. #include "UEvent.h"
  12. #endif
  13.  
  14. //    #ifndef __UGEOMETRY__
  15. //    #include "UGeometry.h"
  16. //    #endif
  17.  
  18. // Toolbox
  19.  
  20. #ifndef __APPLEEVENTS__
  21. #include <AppleEvents.h>
  22. #endif
  23.  
  24. //----------------------------------------------------------------------------------------
  25. // Forward and external class declarations. 
  26. //----------------------------------------------------------------------------------------
  27.  
  28. class TAppleEvent;
  29. class TCommand;
  30. class TCommandHandler;
  31. //    class TScroller;
  32. class TView;
  33.  
  34. //----------------------------------------------------------------------------------------
  35. // Some typedefs
  36. //----------------------------------------------------------------------------------------
  37.  
  38. typedef EventPriority CommandPriority;
  39.  
  40. //----------------------------------------------------------------------------------------
  41. // Some constants
  42. //----------------------------------------------------------------------------------------
  43.  
  44. const Boolean kCanUndo = TRUE;
  45. const Boolean kCantUndo = FALSE;
  46. const Boolean kCausesChange = TRUE;
  47. const Boolean kDoesNotCauseChange = FALSE;
  48.  
  49. //    enum ELinkIdentity
  50. //    {
  51. //        kNotLinked,
  52. //        kPrimary,
  53. //        kSecondary
  54. //    };
  55.  
  56. //----------------------------------------------------------------------------------------
  57. // Global Function Prototypes
  58. //----------------------------------------------------------------------------------------
  59.  
  60. TCommand* CommitACommand(TCommand *aCommand);
  61.  
  62. //----------------------------------------------------------------------------------------
  63. // TCommand:Handles a user command, including undoing and mouse-tracking if required.
  64. // Usually created by the part of the Application with the greatest specific knowledge
  65. // about the action to take in response to events and passed back to be performed at
  66. // several well defined places.
  67. //----------------------------------------------------------------------------------------
  68.  
  69. class TCommand : public TEvent
  70. {
  71.     MA_DECLARE_CLASS;
  72.     
  73. public:
  74.     TCommand();
  75.         // Empty constructor to satisfy compiler.
  76.         
  77.     //------------------------------------------------------------------------------------
  78.     // Init & Free  
  79.     //------------------------------------------------------------------------------------
  80.  
  81.     void ICommand(CommandNumber itsCommandNumber,
  82.                                  TCommandHandler* itsContext,
  83.                                  Boolean canUndo,
  84.                                  Boolean causesChange,
  85.                                   TObject* objectToNotify);
  86.         // Initialize a command procedurally.  
  87.  
  88.     virtual ~TCommand();
  89.         // Frees fClipboardView and fUndoClipboardView when appropriate
  90.         
  91.     virtual void AbandonClipboardView();
  92.         // Frees fClipboardView when appropriate
  93.         
  94.     virtual void AbandonUndoClipboardView();
  95.         // Frees fUndoClipboardView when appropriate
  96.  
  97.     virtual void PrepareForUndoRedo();
  98.         // Make the command context visible for undo/redo. 
  99.     
  100.     virtual void FinishUndoRedo();
  101.         // Common undo/redo code. 
  102.  
  103.     virtual void RevealUndoRedo();
  104.         // Make this context visible after undo/redo. 
  105.  
  106.     //------------------------------------------------------------------------------------
  107.     // command processing  
  108.     //------------------------------------------------------------------------------------
  109.  
  110.     virtual void Process(); // override
  111.         // Performs the command (instead of processing as an event). 
  112.     
  113.     virtual Boolean NeedsToUnloadAllSegments();
  114.         // Default behavior is to return true
  115.  
  116.     //    void LinkToSecondary(TCommand *secondaryCommand);
  117.     //        // Create a linked command pair with "this" as the primary command
  118.         
  119.     virtual void SetValidationError(OSErr validationError);
  120.         // Set the validation state of this command
  121.         
  122.     virtual void Abort();
  123.         // Clean up after command fails. 
  124.         
  125.     //------------------------------------------------------------------------------------
  126.     // command processing - usually overridden   
  127.     //------------------------------------------------------------------------------------
  128.  
  129.     //    virtual void Commit();
  130.     //        // Stub  
  131.  
  132.     virtual void DoIt();
  133.         // Stub  
  134.  
  135.     virtual void RedoIt();
  136.         // Stub  
  137.  
  138.     virtual void UndoIt();
  139.         // Stub  
  140.  
  141.     virtual void DoNotification();
  142.         // Default calls fNotify->Changed if fCausesChange is true
  143.     
  144.     virtual void SetupDependencies();
  145.         // Undoable commands are dependents of their contexts. When 
  146.         // the context is changed, the command is committed. 
  147.  
  148.     virtual void DoUpdate(ChangeID theChange,
  149.                           TObject* changedObject, 
  150.                           TObject* changedBy,
  151.                           TDependencySpace* dependencySpace); // override
  152.         // If the context closed, the command gets committed
  153.  
  154.     virtual void Completed();
  155.         // Called after the command's DoIt method has been called. Also called if
  156.         // failure occurs during the process of DoIt, UndoIt, or RedoIt.
  157.     
  158.     //------------------------------------------------------------------------------------
  159.     // Clipboard handling  
  160.     //------------------------------------------------------------------------------------
  161.  
  162.     virtual void ClaimClipboard(TView* clipboardView);
  163.         // Installs the specified view in the clipboard window. Saves the current
  164.         // clipboard view for purposes of Undo
  165.         
  166.     virtual void UndoRedoClipboard();
  167.         // Installs the appropriate view in the clipboard window
  168.         
  169.     //------------------------------------------------------------------------------------
  170.     // Scheduling  
  171.     //------------------------------------------------------------------------------------
  172.  
  173.     virtual Boolean CanBeUndone();
  174.         // Default behavior is to return the value stored in fCanUndo  
  175.  
  176.     virtual Boolean IsReadyToExecute();
  177.         // Returns true (the Default) when the command is ready to execute. Override to
  178.         // base the execution of the command on more sophisticated criteria. Most programs
  179.         // won't have to mess with this..  
  180.  
  181.     virtual Boolean ShouldFreeOnCompletion();
  182.         // Overridden to return the value stored in fFreeOnCompletion  
  183.  
  184.     virtual Boolean IsRecurring();
  185.         // Overridden to return the value stored in fRecurring  
  186.  
  187.     virtual Boolean WillCauseChange();
  188.         // Default behavior is to return the value stored in fCausesChange
  189.         
  190.     virtual TAppleEvent* MakeAppleEvent();  
  191.         // Make an Apple Event corresponding to the command. Override for your commands. 
  192.  
  193.     //------------------------------------------------------------------------------------
  194.     // Change notification  
  195.     //------------------------------------------------------------------------------------
  196.  
  197.     virtual ChangeID GetChangeID();
  198.         // Returns a ChangeID for TApplication::PerformCommand to pass to Changed. Returns
  199.         // fIdentifier as a default.  
  200.  
  201.     //------------------------------------------------------------------------------------
  202.     // data members  
  203.     //------------------------------------------------------------------------------------
  204. public:
  205.  
  206.     TObject* fObjectToNotify;                    // object to notify of changes  
  207.  
  208.     TCommandHandler* fContext;                    // A document or view. If the command is
  209.                                                 // undoable it will be committed when the
  210.                                                 // context is closed  
  211.                                           
  212.     TView* fClipboardView;                        // The view installed in the clipboard by this 
  213.                                                 // command in its DoIt and RedoIt phases
  214.     TView* fUndoClipboardView;                    // The view installed in the clipboard by this 
  215.                                                 // command in its UndoIt phase
  216.     TCommandHandler* fUndoClipboardViewContext; // The context of the command which originally 
  217.                                                 // placed the current saved fUndoClipboardView
  218.                                                 // in the clipboard window
  219.  
  220.     OSErr fValidationError;                        // noErr (the default) indicates that neither
  221.                                                 // this command nor a linked command have
  222.                                                 // failed.
  223.     
  224.     //    TCommand* fLinkedCommand;                    // Associated command that does and undoes in synch.
  225.     //                                                // Used by drag and drop.
  226.                                                 
  227.     //    ELinkIdentity fLinkIdentity;                // Identifies command as unlinked or as the primary
  228.     //                                                // or secondary command in a command pair.
  229.  
  230.     Boolean fCommandDone;                        // "Done" as in DoIt/UndoIt/RedoIt. 
  231.  
  232.     Boolean fCanUndo;                            // Defaults to true  
  233.  
  234.     Boolean fCausesChange;                        // Defaults to true; Marks document
  235.                                                 // changed when command is done  
  236.  
  237.     Boolean fFreeOnCompletion;                    // True to cause the command to be freed
  238.                                                 // on completion (the default). Completion
  239.                                                 // is after DoIt for non-undoable commands
  240.                                                 // and Commit for undoable commands.  
  241.  
  242.     Boolean fReadyToExecute;                    // True (the default) will enable this
  243.                                                 // command to be returned from any queues
  244.                                                 // that may be holding it. Most programs
  245.                                                 // won't have to mess with this.  
  246.  
  247.     Boolean fRecurring;                            // False (the default) will enable this
  248.                                                 // command to be removed from any command
  249.                                                 // queue that holds it because it is only
  250.                                                 // executed once.
  251.                                                 
  252.     Boolean    fUseAppleEvent;                        // True if the command should be handled by
  253.                                                 // sending an Apple Event
  254.     
  255.     Boolean fChangesClipboard;                    // Defaults to false. Set to true for
  256.                                                 // command subclasses representing CUT and/
  257.                                                 // or COPY commands which change the
  258.                                                 // Clipboard.  
  259.  
  260. };
  261.  
  262.  
  263. //----------------------------------------------------------------------------------------
  264. // TAppleCommand: Abstract class for TServerCommand and TClientCommand.
  265. //----------------------------------------------------------------------------------------
  266.  
  267. class TAppleCommand : public TCommand
  268. {
  269.     MA_DECLARE_CLASS;
  270.     
  271. public:
  272.  
  273.     TAppleCommand();
  274.         // Constructor
  275.         
  276.     void IAppleCommand(CommandNumber itsCommandNumber,
  277.                                       TCommandHandler* itsContext,
  278.                                       Boolean canUndo,
  279.                                       Boolean causesChange,
  280.                                       TObject* objectToNotify);
  281.     void IAppleCommand(CommandNumber itsCommandNumber,
  282.                      TCommandHandler* itsContext,
  283.                      Boolean canUndo,
  284.                      Boolean causesChange,
  285.                      TObject* objectToNotify,
  286.                      TAppleEvent* message,
  287.                      TAppleEvent* reply);
  288.  
  289.  
  290.     virtual ~TAppleCommand();
  291.     
  292.     virtual void FreeTheMessage();
  293.         // Frees fMessage and fReply.
  294.         
  295. //----------------------------------------------------------------------------------------
  296. // data members
  297. //----------------------------------------------------------------------------------------
  298. public:
  299.  
  300.     TAppleEvent* fMessage;
  301.     TAppleEvent* fReply;
  302.  
  303. };
  304.  
  305.  
  306. //----------------------------------------------------------------------------------------
  307. // TServerCommand: Should be used to service an AppleEvent.  It should also be used for all
  308. // commands that can be invoked by an AppleEvent _OR_ some user event (DoMenuCommand).
  309. // A good example is the 'odoc' command. The 'odoc' command's DoIt method simply opens
  310. // a list of files. TServerCommand lets that list come from an AppleEvent or from some user
  311. // action.
  312. //----------------------------------------------------------------------------------------
  313.  
  314. class TServerCommand : public TAppleCommand
  315. {
  316.     MA_DECLARE_CLASS;
  317.     
  318. public:
  319.  
  320.     TServerCommand();
  321.         // Constructor
  322.     virtual ~TServerCommand();
  323.         // Destructor
  324.         
  325.     void IServerCommand(CommandNumber itsCommandNumber,
  326.                         TCommandHandler* itsContext,
  327.                         Boolean canUndo,
  328.                         Boolean causesChange,
  329.                         TObject* objectToNotify);
  330.         // Initialize a TServerCommand to function independently (no message
  331.         // or reply). 
  332.                                       
  333.     void IServerCommand(CommandNumber itsCommandNumber,
  334.                         TCommandHandler* itsContext,
  335.                         Boolean canUndo,
  336.                         Boolean causesChange,
  337.                         TObject* objectToNotify,
  338.                         const AppleEvent& itsMessage,
  339.                         const AppleEvent& itsReply);
  340.         // Initialize a TServerCommand from Apple Events. 
  341.         
  342.     void IServerCommand(CommandNumber itsCommandNumber,
  343.                         TCommandHandler* itsContext,
  344.                         Boolean canUndo,
  345.                         Boolean causesChange,
  346.                         TObject* objectToNotify,
  347.                         TAppleEvent* itsMessage,
  348.                         TAppleEvent*itsReply);
  349.         // Initialize a TServerCommand from MacApp TAppleEvent objects. 
  350.         // Replaces the old InitializeFromAppleEvent method. 
  351.                                       
  352.     virtual void Process();             // override
  353.         // Check the state of fRequiresUserInteraction and call AEInteractWithUser
  354.         // if necessary.  Report any errors back to the AppleEvent manager.
  355.     
  356.     void ResumeAndFreeMessage();
  357.         // If the event has been suspended then resume it and free the message.
  358.     
  359.     virtual void SetValidationError(OSErr error);
  360.         // Report any errors in the supplied reply.
  361.         
  362.     virtual void Completed();        // override
  363.         // Called when the command's DoIt method has been called, and after commit
  364.         // if undoable.  Also called if failure ocurred during the process of doing
  365.         // the command.  Overridden to Resume the suspended AppleEvent.
  366.  
  367. //----------------------------------------------------------------------------------------
  368. // data members
  369. //----------------------------------------------------------------------------------------
  370. public:
  371.  
  372.     Boolean fRequiresUserInteraction;        // If true AEInteractWithUser will be called
  373.                                             // before the command is processed
  374.     Boolean fSuspendTheEvent;                // If true AESuspendTheCurrentEvent will be
  375.                                             // called before the command is processed
  376.         
  377. protected:
  378.     Boolean fReplySent;        // Set to TRUE in completed if an fReply was sent.
  379.  
  380. };
  381.  
  382.  
  383. //----------------------------------------------------------------------------------------
  384. // TClientCommand: can be used to send an AppleEvent.  If the message is to be sent
  385. // kAENoReply then the default behavior is to send its fMessage in the DoIt Method.
  386. // kAEWaitReply will send the message and process the reply before being posted to
  387. // the event queue for normal command processing.  kAEQueueReply will send the message
  388. // and then place itself in a pending queue until its matching reply is received.  Then it
  389. // will process the reply and allow itself to be posted to the event queue for normal
  390. // processing.
  391. //----------------------------------------------------------------------------------------
  392.  
  393. class TClientCommand : public TAppleCommand
  394. {
  395.     MA_DECLARE_CLASS;
  396.     
  397. public:
  398.  
  399.     TClientCommand();
  400.         // Constructor
  401.     virtual ~TClientCommand();
  402.         // Destructor
  403.         
  404.     void IClientCommand(CommandNumber itsCommandNumber,
  405.                         TCommandHandler* itsContext,
  406.                         Boolean canUndo,
  407.                         Boolean causesChange,
  408.                         TObject* objectToNotify,
  409.                         TAppleEvent* theMessage = NULL);
  410.                                       
  411.     virtual long GetReturnID();
  412.         // Return the return ID of fMessage.  Used to match an incoming reply with
  413.         // this command.
  414.                                       
  415.     virtual Boolean IsReadyToPost();    // override
  416.         // check that message has been sent and the reply received before posting the
  417.         // command to the event queue.
  418.                                       
  419.     virtual void ProcessReply(TAppleEvent* theReply);
  420.         // Assign theReply to fReply.  Optionally one could process the contents of the
  421.         // reply here and assign them to local instance variables
  422.  
  423.     virtual TAppleEvent* SendMessage();
  424.         // Send the message stored in fMessage and return the message if one is returned
  425.  
  426.     virtual void DoIt();    // override
  427.         // Default behavior for a kAENoReply message is to send the message
  428.         // If your message is to be sent by WaitReply or QueueReply you should override
  429.         // DoIt to do your own thing
  430.  
  431. //----------------------------------------------------------------------------------------
  432. // data members
  433. //----------------------------------------------------------------------------------------
  434. public:
  435.  
  436.     Boolean fMessageSent;
  437.     
  438. };
  439.  
  440.  
  441. #endif // __UCOMMAND__